;(function(){

'use strict';

angular.module('controllers.userManager.customization')
	.controller('customizationCtrl', customizationCtrl);

function customizationCtrl($rootScope, $scope, customizationService, CoreService, $cookies,$state) {
	$scope.user = $cookies.getObject('userCookie');
	var myModules = customizationService.initGetModules().userModules;
	var m ;
	setTimeout(function(){
		m = angular.copy(myModules);
	},1000);
	$scope.flag = {
		show:false
	};
	//初始化查询,匹配该用户是否有该模块,如果有,则选中
	$scope.initCheck = function() {
		var leng = {};
		var userIds = [];
		var myParams = {
			userId:$cookies.getObject('userCookie').userId
		};
		customizationService.getModules(myParams,function(res){
			angular.forEach(res.data.userModules, function(user) {
				leng[user.moduleId] = user.childModule.length;
				if (userIds.indexOf(user.moduleId) === -1 && user.childModule.length !== 0) {
					userIds.push(user.moduleId);
				}
				angular.forEach(user.childModule, function(child) {
					if (userIds.indexOf(child.moduleId) === -1 ) {
						userIds.push(child.moduleId);
					}
				});
			});
			angular.forEach(res.data.allModules, function(all) {
				//只有当模块下所有的子模块选中后父模块才会被勾选
				if (userIds.indexOf(all.moduleId) !== -1 && leng[all.moduleId] === all.childModule.length) {
					all.checked = true;
				}
				angular.forEach(all.childModule, function(parent) {
					parent.moduleId === 200551 ? parent.show = false : parent.show = true;
					if (userIds.indexOf(parent.moduleId) !== -1) {
						parent.checked = true;
					}
				});
			});
			$scope.allModules = res.data.allModules;
			$scope.flag.show = true;
		});
	};
	$scope.checkedParent = checkedParent;
	$scope.checkedChild = checkedChild;
	$scope.save = save;
	$scope.reset = function() {
		myModules = m;
		$scope.initCheck();
	};

	//选中父节点
	function checkedParent(parent){
		angular.forEach(parent.childModule, function(subChild) {
			subChild.checked = parent.checked;
			//当模块为个人页面定制和无用库作业表清理时当前状态只能是选中
			if(subChild.moduleId === 48 || subChild.moduleId === 205){
				subChild.checked = true;
			}
			if (parent.checked) {
				if (myModules[parent.moduleId].indexOf(parent.moduleId) === -1) {
					myModules[parent.moduleId].push(parent.moduleId);
				}
				addModule(parent, subChild.moduleId);
			} else {
				if (myModules[parent.moduleId].indexOf(parent.moduleId) !== -1) {
					myModules[parent.moduleId].splice(myModules[parent.moduleId].indexOf(parent.moduleId), 1);
				}
				delModule(parent, subChild.moduleId);
			}
		});
	}
	//选中子节点
	function checkedChild(parent,sub){
		parent.checked = sub.checked; //将子节点的值赋给父节点，如果为false，则父节点也为false
		if (parent.checked) {
			//如果为true 判断父节点里所有子节点是否都为选中状态
			angular.forEach(parent.childModule, function(subChild) {
				if (!subChild.checked) {
					parent.checked = false; //有一个没选中，则父节点也不能被选中
				}
			});
			//将选中的节点添加到模块中
			addModule(parent, sub.moduleId);
		} else {
			//移除选中的节点
			delModule(parent, sub.moduleId);
		}
	}
	//删除模块
	function delModule(parent, id) {
		//找到相同就删除
		angular.forEach(myModules[parent.moduleId], function(moduleId, index) {
			if (id === moduleId) {
				myModules[parent.moduleId].splice(index, 1);
			}
		});
	}
	//新增模块
	function addModule(parent, id) {
		var has = false;
		//如果找到相同的ID，就添加到数组中
		angular.forEach(myModules[parent.moduleId], function(moduleId) {
			if (id === moduleId) {
				has = true;
			}
		});
		has || myModules[parent.moduleId].push(id);
		//选择子模块的时候将父模块也添加进去
		if(myModules[parent.moduleId].length >=1 && myModules[parent.moduleId].indexOf(parent.moduleId) === -1){
			myModules[parent.moduleId].push(parent.moduleId);
		}
	}
	//保存
	function save(){
		var param = {
			moduleIds: [],
			userId: $cookies.getObject('userCookie').userId
		};
		angular.forEach(myModules, function(item) {
			if(item.length !== 1){
				item.map(function(ids) {
					param.moduleIds.push(ids);
				});
			}
		});
		//保留个人页面定制模块和无用库作业表清理模块
		// param.moduleIds = defaultModuleId(param,[5,48,200,205]);
		customizationService.saveModules(param,function(res){
			if(res.state === '10000'){
				alert('保存成功');
				if($rootScope.customizationTitle === 'customization'){
					$state.go('sas.userAll');
				}else {
					$state.go('sas.dataQualityMng');
				}

				$rootScope.module = customizationService.initGetModules();
			}
		});
	}
	function defaultModuleId(param,idArr){
		angular.forEach(idArr,function(id){
			if(param.moduleIds.indexOf(id) === -1){
				param.moduleIds.push(id);
			}
		});
		return param.moduleIds;
	}
}
customizationCtrl.$inject = ['$rootScope', '$scope', 'customizationService', 'CoreService', '$cookies','$state'];

})();